package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author luojia
 * @create 2022-01-01 23:17
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    /**
     * 客户列表分页（名称模糊查询）
     *
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public String queryCustomer(Integer page, Integer rows, String customerName) {
        Integer initialRow = (page - 1) * rows;
        List<Customer> customerList = customerDao.queryCustomer(initialRow, rows, customerName);

        int size = customerList.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", size);
        map.put("rows", customerList);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    /**
     * 客户添加或修改
     *
     * @param customer
     * @param customerId
     * @return
     */
    @Override
    public void customerAddOrUpdate(Customer customer, Integer customerId) {
        if (customerId == null) {
            // 添加
            customerDao.addCustomer(customer);
        } else {
            // 更新
            customerDao.updateCustomer(customer, customerId);
        }
    }

    /**
     * 客户删除（支持批量删除）
     *
     * @param split
     * @return
     */
    @Override
    public void customerDelete(String[] split) {
        customerDao.customerDelete(split);
    }

     /**
     * 客户下拉列表
     * @param name
     * @return
     */
    @Override
    public List<Customer> getComboboxList(String name) {
        return customerDao.findCustomerList(name);
    }
}
